#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

jq --version > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "Please Install 'jq' https://stedolan.github.io/jq/ to execute this script"
	echo
	exit 1
fi
starttime=$(date +%s)

echo "POST request Enroll on Org1  ..."
echo
ORG1_TOKEN=$(curl -s -X POST \
  http://localhost:4000/users \
  -H "content-type: application/x-www-form-urlencoded" \
  -d 'username=Jim&orgname=org1')
echo $ORG1_TOKEN
echo
echo
echo "POST request Enroll on Org2 ..."
echo
ORG2_TOKEN=$(curl -s -X POST \
  http://localhost:4000/users \
  -H "content-type: application/x-www-form-urlencoded" \
  -d 'username=Barry&orgname=org2')
echo $ORG2_TOKEN
echo
echo
echo
echo "POST request Create channel  ..."
echo
curl -s -X POST \
  http://localhost:4000/channels \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"channelName":"mychannel",
	"channelConfigPath":"../artifacts/channel/mychannel.tx"
}'
echo
echo
sleep 5
echo "POST request Join channel on Org1"
echo
curl -s -X POST \
  http://localhost:4000/channels/mychannel/peers \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"peers": ["localhost:7051","localhost:7056"]
}'
echo
echo

# echo
# echo "POST Install chaincode Account on Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"peers": ["localhost:7051","localhost:7056"],
# 	"chaincodeName":"account",
# 	"chaincodePath":"github.com/Account",
# 	"chaincodeVersion":"v0"
# }'
# echo
# echo


echo "POST Install chaincode  DigitalAsset on Org1"
echo
curl -s -X POST \
  http://localhost:4000/chaincodes \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"peers": ["localhost:7051","localhost:7056"],
	"chaincodeName":"digitalAsset",
	"chaincodePath":"github.com/DigitalAsset",
	"chaincodeVersion":"v0"
}'
echo
echo


# echo "POST Install chaincode  EnergyDigitalAsset on Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"peers": ["localhost:7051","localhost:7056"],
# 	"chaincodeName":"energyDigitalAsset",
# 	"chaincodePath":"github.com/EnergyDigitalAsset",
# 	"chaincodeVersion":"v0"
# }'
# echo
# echo


echo "POST Install chaincode  MoneyAndCoin on Org1"
echo
curl -s -X POST \
  http://localhost:4000/chaincodes \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"peers": ["localhost:7051","localhost:7056"],
	"chaincodeName":"moneyAndCoin",
	"chaincodePath":"github.com/MoneyAndCoin",
	"chaincodeVersion":"v0"
}'
echo
echo


# echo "POST Install chaincode  TransactionRecord on Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"peers": ["localhost:7051","localhost:7056"],
# 	"chaincodeName":"transactionRecord",
# 	"chaincodePath":"github.com/TransactionRecord",
# 	"chaincodeVersion":"v0"
# }'
# echo
# echo


# echo "POST instantiate chaincode account on peer1 of Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/channels/mychannel/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"chaincodeName":"account",
# 	"chaincodeVersion":"v0",
# 	"functionName":"init",
# 	"args":["admin","admin"]
# }'
# echo
# echo

echo "POST instantiate chaincode digitalAsset on peer1 of Org1"
echo
curl -s -X POST \
  http://localhost:4000/channels/mychannel/chaincodes \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"chaincodeName":"digitalAsset",
	"chaincodeVersion":"v0",
	"functionName":"init",
	"args":["admin","admin"]
}'
echo
echo

# echo "POST instantiate chaincode energyDigitalAsset on peer1 of Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/channels/mychannel/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"chaincodeName":"energyDigitalAsset",
# 	"chaincodeVersion":"v0",
# 	"functionName":"init",
# 	"args":["admin","admin"]
# }'
# echo
# echo

echo "POST instantiate chaincode moneyToCoin on peer1 of Org1"
echo
curl -s -X POST \
  http://localhost:4000/channels/mychannel/chaincodes \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1",
	"chaincodeName":"moneyAndCoin",
	"chaincodeVersion":"v0",
	"functionName":"init",
	"args":["admin","2","999999"]
}'
echo
echo

# echo "POST instantiate chaincode transactionRecord on peer1 of Org1"
# echo
# curl -s -X POST \
#   http://localhost:4000/channels/mychannel/chaincodes \
#   -H "content-type: application/json" \
#   -d '{
#   "username":"Jim",
#   "orgname":"org1",
# 	"chaincodeName":"transactionRecord",
# 	"chaincodeVersion":"v0",
# 	"functionName":"init",
# 	"args":["admin","admin"]
# }'
# echo
# echo


echo "GET query ChainInfo"
echo
curl -s -X GET \
  "http://localhost:4000/channels/mychannel?peer=peer1" \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1"
}'
echo
echo

echo "GET query Installed chaincodes"
echo
curl -s -X GET \
  "http://localhost:4000/chaincodes?peer=peer1&type=installed" \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1"
}'
echo
echo

echo "GET query Instantiated chaincodes"
echo
curl -s -X GET \
  "http://localhost:4000/chaincodes?peer=peer1&type=instantiated" \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1"
}'
echo
echo

echo "GET query Channels"
echo
curl -s -X GET \
  "http://localhost:4000/channels?peer=peer1" \
  -H "content-type: application/json" \
  -d '{
  "username":"Jim",
  "orgname":"org1"
}'
echo
echo


echo "Total execution time : $(($(date +%s)-starttime)) secs ..."
